package educrypt.gui.ctr;

import educrypt.database.EduCryptDatabase;
import educrypt.database.dao.AlphabetDAO;
import educrypt.database.dao.FrequencyAnalysisDAO;
import educrypt.database.dao.KeywordDAO;
import educrypt.database.dao.TextDAO;
import educrypt.database.dao.TextLanguageDAO;
import educrypt.database.dao.TextTypeDAO;
import educrypt.gui.view.AboutView;
import educrypt.gui.view.AnalysisView;
import educrypt.gui.view.CiphersView;
import educrypt.gui.view.ConsoleBigView;
import educrypt.gui.view.ConsoleSmallView;
import educrypt.gui.view.CryptanalysisView;
import educrypt.gui.view.DatabaseView;
import educrypt.gui.view.EditorAlphabetView;
import educrypt.gui.view.EditorKeywordView;
import educrypt.gui.view.EditorTextOptionsView;
import educrypt.gui.view.GoToView;
import educrypt.gui.view.MainView;
import educrypt.log.ObservableLogger;

public class EduCryptCtr {
	private MainView view;

	public EduCryptCtr(MainView view) {
		this.view = view;
		this.bindListeners();
	}

	private void bindListeners() {
		/* Obtaining panels and dialogs */
		GoToView goToView = this.view.getActionPanel();
		AboutView aboutView = this.view.getAboutPanel();
		CiphersView ciphersView = this.view.getCiphersPanel();
		CryptanalysisView cryptanalysisView = this.view.getCryptanalysisPanel();
		AnalysisView analysisView = this.view.getAnalysisPanel();
		DatabaseView databaseView = this.view.getDatabasePanel();

		EditorTextOptionsView editorTextTypeView = this.view
				.getEditorTextTypeDialog();
		EditorTextOptionsView editorTextLanguageView = this.view
				.getEditorTextLanguageDialog();
		EditorKeywordView editorKeywordView = this.view
				.getEditorKeywordDialog();
		EditorAlphabetView editorAlphabetView = this.view
				.getEditorAlphabetDialog();

		ConsoleSmallView consoleCipherView = this.view.getCiphersPanel()
				.getConsolePanel();
		ConsoleSmallView consoleCryptanalysisView = this.view
				.getCryptanalysisPanel().getConsolePanel();
		ConsoleBigView consoleBigView = this.view.getConsoleBigDialog();

		/* Creating controllers */
		GoToCtr goToCtr = new GoToCtr(this.view);
		AboutCtr aboutCtr = new AboutCtr(this.view);
		CiphersCtr ciphersCtr = new CiphersCtr(this.view);
		AnalysisCtr analysisCtr = new AnalysisCtr(this.view);
		CryptanalysisCtr cryptanalysisCtr = new CryptanalysisCtr(this.view);
		DatabaseCtr databaseCtr = new DatabaseCtr(this.view);

		EditorTextTypeCtr editorTextTypeCtr = new EditorTextTypeCtr(this.view);
		EditorTextLanguageCtr editorTextLanguageCtr = new EditorTextLanguageCtr(
				this.view);
		EditorKeywordCtr editorKeywordCtr = new EditorKeywordCtr(this.view);
		EditorAlphabetCtr editorAlphabetCtr = new EditorAlphabetCtr(this.view);

		ConsoleCtr consoleCtr = new ConsoleCtr(this.view);

		/* Listeners binding */
		goToView.addListener(goToCtr);
		aboutView.addListener(aboutCtr);
		ciphersView.addListener(ciphersCtr);
		cryptanalysisView.addListener(cryptanalysisCtr);
		analysisView.addListener(analysisCtr);
		databaseView.addListener(databaseCtr);

		editorTextTypeView.addListener(editorTextTypeCtr);
		editorTextLanguageView.addListener(editorTextLanguageCtr);
		editorKeywordView.addListener(editorKeywordCtr);
		editorAlphabetView.addListener(editorAlphabetCtr);

		consoleCipherView.addListener(consoleCtr);
		consoleCryptanalysisView.addListener(consoleCtr);
		consoleBigView.addListener(consoleCtr);

		/* Obtaining Observable */
		TextTypeDAO textTypeDAO = TextTypeDAO.getInstance();
		TextLanguageDAO textLanguageDAO = TextLanguageDAO.getInstance();
		KeywordDAO keywordDAO = KeywordDAO.getInstance();
		AlphabetDAO alphabetDAO = AlphabetDAO.getInstance();
		TextDAO textDAO = TextDAO.getInstance();
		FrequencyAnalysisDAO frequencyAnalysisDAO = FrequencyAnalysisDAO
				.getInstance();
		ObservableLogger observableLogger = ObservableLogger.getInstance();
		EduCryptDatabase eduCryptDatabase = EduCryptDatabase.getInstance();

		/* Observers binding */
		textTypeDAO.addObserver(analysisCtr);
		textTypeDAO.addObserver(editorTextTypeCtr);
		textTypeDAO.addObserver(cryptanalysisCtr);

		textLanguageDAO.addObserver(analysisCtr);
		textLanguageDAO.addObserver(editorTextLanguageCtr);
		textLanguageDAO.addObserver(cryptanalysisCtr);

		keywordDAO.addObserver(ciphersCtr);
		keywordDAO.addObserver(cryptanalysisCtr);

		alphabetDAO.addObserver(ciphersCtr);
		alphabetDAO.addObserver(analysisCtr);
		alphabetDAO.addObserver(cryptanalysisCtr);
		alphabetDAO.addObserver(databaseCtr);
		alphabetDAO.addObserver(cryptanalysisCtr);

		textDAO.addObserver(databaseCtr);
		frequencyAnalysisDAO.addObserver(databaseCtr);

		eduCryptDatabase.addObserver(analysisCtr);
		eduCryptDatabase.addObserver(ciphersCtr);
		eduCryptDatabase.addObserver(cryptanalysisCtr);
		eduCryptDatabase.addObserver(databaseCtr);

		observableLogger.addObserver(consoleCtr);
	}
}
